const app = getApp();
const COS = require('../../../utils/cos-wx-sdk-v5.js');
const cos = new COS({
  SecretId: 'AKIDYyUjwt34oH1U9YxZ9i13oIU93gDTY9ll',
  SecretKey: 'qQi012oQYOzILccMpKZxYuXtNbYFfpjN'
});
const $api = require('../../../service/api').API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: app.globalData.imgUrl,
    list: [],
    str_orderid: 0,
    massage: '',
    name: '',
    address: '',
    wx: '',
    phone: ''
  },
  goback() {
    wx.navigateBack({
      delta: 1,
    })
  },
  onChangeInput(e) {
    let str = e.detail.value;
    let name = e.currentTarget.dataset.prop;
    if (name == 'username') {
      if (str.length < 2) {
        wx.showToast({
          icon: 'none',
          title: '报修人不能少于2位字符',
          duration: 2000
        })
        return false;
      }
    } else
    if (name == 'address') {
      if (str.length < 4) {
        wx.showToast({
          icon: 'none',
          title: '宿舍地址不能少于4位字符',
          duration: 2000
        })
        return false;
      }
    } else
    if (name == 'wechat') {
      if (str.length < 6) {
        wx.showToast({
          icon: 'none',
          title: '微信号不能少于6位字符',
          duration: 2000
        })
        return false;
      }
    } else
    if (name == 'phone') {
      if (str.length < 11) {
        wx.showToast({
          icon: 'none',
          title: '手机号不能少于11位字符',
          duration: 2000
        })
        return false;
      }
    }


    console.log(e.detail.value); //input 的 value
    console.log(e.currentTarget.dataset.prop); //data-prop 绑定的字符串，以此来确定改变的是哪个变量
    this.setData({
      [e.currentTarget.dataset.prop]: e.detail.value
    });
  },
  getText(e) {

    let str = e.detail.value;
    if (str.length < 4) {
      wx.showToast({
        title: '详情不能少于4个字符',
        icon: 'none',
        duration: 2000
      })
      return false;
    }
    this.setData({
      massage: e.detail.value
    });
    console.log("a" + e.detail.value);
  },
  uploadimg() {
    var _this = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original'], // 可以指定是原图还是压缩图，默认用原图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        let filePath = res.tempFiles[0].path;
        let filename = filePath.substr(filePath.lastIndexOf('/') + 1);
        _this.setData({
          ['fileList.[' + 0 + '].url']: filePath
        });

        cos.postObject({
            Bucket: 'repair-1259727252',
            Region: 'ap-guangzhou',
            Key: filename,
            FilePath: filePath,
            onProgress: function (info) {
              console.log(JSON.stringify(info));
            }
          },
          function (err, data) {
            console.log(err || data.Location);
            wx.setStorageSync('imgUrl', 'https://' + data.Location);
          }
        );
      }
    });
  },
  feedBack() {
    console.log(this.data.massage);
    let that = this;
    const data = {
      openid: wx.getStorageSync('openid'),
      token: wx.getStorageSync('token'),
      orderid: this.data.str_orderid,
      massage: this.data.massage,
      imageUrl: wx.getStorageSync('imgUrl'),
      name: this.data.name,
      address: this.data.address,
      wx: this.data.wx,
      phone: this.data.phone
    }
    $api.feedback(data).then((res) => {
      console.log(res);
      if (res.code == 11) {
        wx.showToast({
          title: res.message,
          icon: 'success',
          duration: 2000
        });
      } else {
        this.goback();
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 2000
        })
      }
    })
    wx.requestSubscribeMessage({
        tmplIds: ['RAzc1IgVnm6CZmKhiZIcHatVjTq0aJxdTO3MPuFO08Y'],
      })
    wx.navigateTo({
      url: '../ModifySuccess/modifySuccess',
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var str_orderid = options.str_orderid;
    let that = this;
    that.setData({
      str_orderid: str_orderid
    });
    console.log(this.data.str_orderid);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})